Using Calculation Setups

Calculation setups are a way to efficiently do LCA calculations for multiple functional units and methods at the same time.

This notebook builds on the Getting started with Brightway2 notebook, and won't work if you haven't done that notebook yet.

In [1]:
from brightway2 import *
In [2]:
if "Calculation setups" not in projects:
    projects.current = "BW2 introduction"
    projects.copy_project("Calculation setups")
else:
    projects.current = "Calculation setups"

Defining a calculation setup

A calculation setups is defined by three things:

  • A name
  • A list of functional units
  • A list of LCIA methods

In this example, we will choose both activities and methods at random.

In [3]:
functional_units = [{Database('forwast').random(): 1} for _ in range(20)]

We can't choose methods completely at random, though, as the forwast database doesn't have as many biosphere flows as ecoinvent, so many methods will only characterize flows that aren't provided anywhere in forwast. So lets only choose from the methods which will have a non-zero LCA score:

In [4]:
import random

all_forwast_flows = {exc.input for ds in Database("forwast") for exc in ds.biosphere()}
suitable_methods = [method 
                    for method in methods 
                    if {cf[0] for cf in Method(method).load()}.intersection(all_forwast_flows)]

print("Can use {} of {} LCIA methods".format(len(suitable_methods), len(methods)))
chosen_methods = random.sample(suitable_methods, 8)
Can use 306 of 665 LCIA methods

A calculation setup is a normal Python dictionary, with keys inv and ia, for the functional units and LCIA methods, respectively.

In [5]:
my_calculation_setup = {'inv': functional_units, 'ia': chosen_methods}

You define a calculation setup by name in the metadata store calculation_setups, similar to the way that LCIA methods are defined.

In [6]:
calculation_setups['some random stuff'] = my_calculation_setup

We can examine what we have just randomly created:

In [7]:
calculation_setups['some random stuff']
Out[7]:
{'ia': [('EDIP2003 w/o LT', 'global warming w/o LT', 'GWP 100a w/o LT'),
  ('EDIP', 'environmental impact', 'global warming, GWP 100a'),
  ('EDIP2003 w/o LT',
   'eutrophication w/o LT',
   'terrestrial eutrophication w/o LT'),
  ('ReCiPe Endpoint (H,A) w/o LT', 'total w/o LT', 'total w/o LT'),
  ('IMPACT 2002+ (Endpoint)', 'ecosystem quality', 'total'),
  ('EDIP2003 w/o LT', 'human toxicity w/o LT', 'via soil w/o LT'),
  ('ReCiPe Midpoint (H) w/o LT', 'climate change w/o LT', 'GWP100 w/o LT'),
  ('EDIP2003 w/o LT', 'global warming w/o LT', 'GWP 500a w/o LT')],
 'inv': [{'_82 Wholesale trade, EU27' (EUR2003, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_16 Sand, gravel and stone from quarry, EU27' (kilogram, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_29 Textiles, EU27' (kilogram, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_80 Infrastructure, excluding buildings, EU27' (EUR2003, GLO, ['Input Output', 'EU27 2003']): 1},
  {'116 Waste treatment, Landfill of waste, Textiles, DK' (kilogram, GLO, ['Input Output', 'Denmark 2003']): 1},
  {'_76 Gas, EU27' (kilogram, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_34 Recycling of waste paper, EU27' (kilogram, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_96 Research and development, EU27' (EUR2003, GLO, ['Input Output', 'EU27 2003']): 1},
  {'131 Recreational and cultural services, EU27' (EUR2003, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_84 Hotels and restaurants, EU27' (EUR2003, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_67 Gas, DK' (kilogram, GLO, ['Input Output', 'Denmark 2003']): 1},
  {'_26 Food preparations n.e.c., EU27' (kilogram, GLO, ['Input Output', 'EU27 2003']): 1},
  {'115 Waste treatment, Waste water treatment, other, EU27' (kilogram, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_44 Rubber and plastic products, EU27' (kilogram, GLO, ['Input Output', 'EU27 2003']): 1},
  {'103 Waste treatment, Composting of food waste, DK' (kilogram, GLO, ['Input Output', 'Denmark 2003']): 1},
  {'138 Household use, Hygiene, EU27' (EUR2003, GLO, ['Input Output', 'EU27 2003']): 1},
  {'100 Waste treatment, Biogasification of food waste, DK' (kilogram, GLO, ['Input Output', 'Denmark 2003']): 1},
  {'141 Household use, Security, EU27' (EUR2003, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_82 Wholesale trade, EU27' (EUR2003, GLO, ['Input Output', 'EU27 2003']): 1},
  {'_28 Tobacco products, EU27' (kilogram, GLO, ['Input Output', 'EU27 2003']): 1}]}

The normal create, update, and delete machanisms apply:

  • To create a new calculation setup, assign by name, as in cell above
  • To replace a calculation setup, just assign new data, i.e. calculation_setups['some random stuff'] = some_new_stuff.
  • To delete a calculation setup, use del, i.e. del calculation_setups['some random stuff'].

Using a calculation setup

Use the MultiLCA class to get LCA results for a calculation setup. Note that this class does all the calculations as soon as you create it.

In [8]:
mlca = MultiLCA('some random stuff')
In [9]:
mlca.results
Out[9]:
array([[  4.00177818e-01,   4.00177818e-01,   3.84578730e-02,
          3.53608645e-02,   6.66150487e-07,   3.70723285e-03,
          3.95235403e-01,   3.75064977e-01],
       [  7.46790333e-03,   7.46790333e-03,   5.81172620e-04,
          6.58034736e-04,   1.06614413e-08,   6.35962942e-05,
          7.39705358e-03,   7.24627173e-03],
       [  8.60137687e+00,   8.60137687e+00,   6.89937280e-01,
          8.18896400e-01,   1.14550467e-05,   5.27938253e-02,
          8.54591539e+00,   7.89153779e+00],
       [  1.09537639e+00,   1.09537639e+00,   6.91077065e-02,
          9.69665563e-02,   1.24224179e-06,   6.81884029e-03,
          1.08480655e+00,   1.06241801e+00],
       [  9.32652133e-01,   9.32652133e-01,   5.08693103e-03,
          4.86878431e-02,   9.06733139e-08,   4.92670746e-04,
          1.01043189e+00,   3.05736725e-01],
       [  3.43428957e-01,   3.43428957e-01,   1.34351899e-02,
          1.75665510e-01,   2.69107423e-07,   1.45173729e-03,
          3.46865502e-01,   2.97925762e-01],
       [ -2.26372137e-01,  -2.26372137e-01,  -4.78956326e-02,
         -2.77682311e-02,  -9.07612733e-07,  -4.65865326e-03,
         -1.99916003e-01,  -3.81516071e-01],
       [  4.21235883e-01,   4.21235883e-01,   2.90967417e-02,
          3.74470436e-02,   5.25464188e-07,   2.88160168e-03,
          4.18182055e-01,   3.99304936e-01],
       [  4.51069136e-01,   4.51069136e-01,   3.03976998e-02,
          3.93316573e-02,   5.56075260e-07,   2.86537829e-03,
          4.48716433e-01,   4.24067565e-01],
       [  6.29424907e-01,   6.29424907e-01,   6.63172892e-02,
          5.07522178e-02,   1.00318348e-06,   3.63371498e-03,
          6.30113674e-01,   5.32191601e-01],
       [  1.69410590e-01,   1.69410590e-01,   1.95909858e-02,
          1.49539562e-01,   3.21183062e-07,   1.91932036e-03,
          1.69039361e-01,   1.64691741e-01],
       [  7.94002342e+00,   7.94002342e+00,   9.50345606e-01,
          6.05495267e-01,   1.35995724e-05,   4.22858723e-02,
          7.95049926e+00,   6.23784191e+00],
       [  2.19376746e+00,   2.19376746e+00,   3.23193795e-02,
          1.30153019e-01,   7.23921447e-07,   3.52554610e-03,
          2.32348159e+00,   1.12948246e+00],
       [  6.15601567e+00,   6.15601567e+00,   3.89863809e-01,
          6.47776693e-01,   7.37651170e-06,   3.90005847e-02,
          6.10133966e+00,   5.76816530e+00],
       [  2.21078772e-01,   2.21078772e-01,   1.84054500e+00,
         -2.87778730e-03,   2.08367478e-05,   7.38377397e-04,
          2.36142470e-01,   1.10570679e-01],
       [  2.88934724e+00,   2.88934724e+00,   9.59046060e-02,
          2.60169427e-01,   1.91430539e-06,   9.55830093e-03,
          2.87734264e+00,   2.75179846e+00],
       [ -1.92673671e+00,  -1.92673671e+00,   3.78306189e-01,
         -1.79995249e-01,   4.00169846e-06,  -4.63055283e-03,
         -1.88548100e+00,  -2.21232813e+00],
       [  3.23987930e-01,   3.23987930e-01,   2.63295800e-02,
          2.85878150e-02,   4.69881816e-07,   2.60683558e-03,
          3.20954134e-01,   3.07202429e-01],
       [  4.00177818e-01,   4.00177818e-01,   3.84578730e-02,
          3.53608645e-02,   6.66150487e-07,   3.70723285e-03,
          3.95235403e-01,   3.75064977e-01],
       [  6.69887127e+00,   6.69887127e+00,   8.90624399e-01,
          5.31104943e-01,   1.25829844e-05,   3.62466152e-02,
          6.68314188e+00,   5.43686704e+00]])

Visualizing the results

There isn't any code built into Brightway2 yet, but Seaborn has some great visualizations for this type of result array.

In [10]:
%matplotlib inline
In [12]:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
In [18]:
# From https://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html
cmap = sns.cubehelix_palette(8, start=.5, rot=-.75, as_cmap=True) 

sns.heatmap(
    mlca.results / np.average(mlca.results, axis=0), # Normalize to get relative results
    annot=True, 
    linewidths=.05, 
    cmap=cmap, 
    xticklabels=["\n".join(x) for x in mlca.methods]
)
plt.xticks(rotation=70) 
Out[18]:
(array([ 0.5,  1.5,  2.5,  3.5,  4.5,  5.5,  6.5,  7.5]),
 <a list of 8 Text xticklabel objects>)